﻿/*
* edirauth - http://edirauth.codeplex.com
* A .NET library and utility for authenticating to eDirectory
* Copyright (c) 2013 Matt Shirilla (http://shirilla.com)
* Licensed under the Microsoft Public License (MS-RL)
* http://edirauth.codeplex.com/license
*/

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using edirAuth;
using Novell.Directory.Ldap;


namespace edirAuthTests
{
    /// <summary>
    /// Create a new instance of EdirAuthorizer for each test in this class 
    /// as a test may change the server or other info.
    /// </summary>
    [TestClass]
    public class BindTests
    {
        EdirAuthorizer eDirAuthorizer;

        /// <summary>
        /// Test should use all the correct information and the bind should succeed.
        /// </summary>
        [TestMethod]
        public void BindDefaultCredentials() {
            eDirAuthorizer = Helper.EdirAuthorizerFactory();
            AuthInfo authInfo = new AuthInfo();
            authInfo = eDirAuthorizer.connectAndBind(authInfo);
            Assert.AreEqual(ResultCodes.OK, authInfo.BindResult.Code, "Bind failed or wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_DESC, authInfo.BindResult.Desc, "Bind failed or wrong code description was returned.");
        }

        /// <summary>
        /// Test should use the wrong password and the bind should fail
        /// </summary>
        [TestMethod]
        public void BindUseBadPassword() {
            eDirAuthorizer = Helper.EdirAuthorizerFactory();
            AuthInfo authInfo = new AuthInfo();
            eDirAuthorizer.BindPass = "bad pass";
            authInfo = eDirAuthorizer.connectAndBind(authInfo);
            Assert.AreEqual(LdapException.INVALID_CREDENTIALS, authInfo.BindResult.Code, "The bind result should have been LdapException.INVALID_CREDENTIALS");
        }

        /// <summary>
        /// Test should use the wrong user and the bind should fail
        /// </summary>
        [TestMethod]
        public void BindUseBadDN()  {
            eDirAuthorizer = Helper.EdirAuthorizerFactory();
            AuthInfo authInfo = new AuthInfo();
            eDirAuthorizer.BindUser = Helper.INVALIDBINDUSER;
            authInfo = eDirAuthorizer.connectAndBind(authInfo);
            Assert.AreEqual(LdapException.INVALID_CREDENTIALS, authInfo.BindResult.Code, "The bind result should have been LdapException.INVALID_CREDENTIALS");
        }

        /// <summary>
        /// Test should use an invalid format for the dn and the bind should fail
        /// </summary>
        [TestMethod]
        public void BindUseInvalidDNformat() {
            eDirAuthorizer = Helper.EdirAuthorizerFactory();
            AuthInfo authInfo = new AuthInfo();
            eDirAuthorizer.BindUser = Helper.INVALIDBINDUSERFORMAT;
            authInfo = eDirAuthorizer.connectAndBind(authInfo);
            Assert.AreEqual(LdapException.INVALID_DN_SYNTAX, authInfo.BindResult.Code, "The bind result should have been LdapException.INVALID_DN_SYNTAX");
        }

        /// <summary>
        /// Test should use an invalid server name and bind should fail
        /// </summary>
        [TestMethod]
        public void BindUseBadServerName() {
            eDirAuthorizer = Helper.EdirAuthorizerFactory();
            AuthInfo authInfo = new AuthInfo();
            eDirAuthorizer.LdapServer = Helper.INVALIDBINDSERVER;
            authInfo = eDirAuthorizer.connectAndBind(authInfo);
            Assert.AreEqual(ResultCodes.NON_LDAP_ERR, authInfo.BindResult.Code, "The bind should have failed.");
        }

        

    }//class
}// namespace
